package com.example.suanfa;

public class SelectionSort {
    public static void main(String[] args) {
        //排序算法
        int[] arr = {5, 7, 3, 9, 1, 2, 6, 4, 8};
        for (int i = 0; i < arr.length-1; i++) {
            int minPos = i;
            for (int j = i+1; j < arr.length; j++) {
                minPos = arr[j] < arr[minPos] ? j : minPos;
            }
//            System.out.println("minPose:" + minPos);
            swap(arr,i,minPos);
//            System.out.println("经过第" + i + "次循环之后，数组的内容：");
            for (int k = 0; k < arr.length; k++) {
                System.out.print(arr[k] + " ");
            }
        }
//        System.out.println();
        print(arr);
    }
    static void sort(int[] arr) {
        for (int i = 0; i < arr.length-1; i++) {
            int minPos = i;
            for (int j = i+1; j < arr.length; j++) {
                minPos = arr[j] < arr[minPos] ? j : minPos;
            }
//            System.out.println("minPose:" + minPos);
            swap(arr,i,minPos);
        }
        System.out.println();
        print(arr);
    }
    static void swap(int[] arr,int i, int j){
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }

    static void print(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + "  ");
        }
    }
}
